package com.JavaBasic;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import javax.sound.sampled.Port;
import javax.xml.transform.OutputKeys;

public class SocketReceive {
	public static void main(String[] args) throws IOException {
		DatagramSocket sc = new DatagramSocket(8888);

		DatagramPacket pk = new DatagramPacket(new byte[1024], 1024);
		
		while(true) {
			sc.receive(pk);
			// 获取收到消息的长度
			int msgLen = pk.getLength();
			// 获取发送消息的主机地址
			String hoString = pk.getAddress().getHostAddress();
			// 获取发送消息的端口号
			int port = pk.getPort();
			String rcMsg = new String(pk.getData(),0,msgLen);
			System.out.println(hoString + ":" + port + " " + rcMsg);
		}
	}
}
